package com.haredot.controller;

import com.github.pagehelper.Page;
import com.haredot.annotation.DenyStudent;
import com.haredot.entity.Course;
import com.haredot.entity.Mark;
import com.haredot.entity.Student;
import com.haredot.entity.Teacher;
import com.haredot.service.TeacherService;
import com.haredot.vo.LayuiPage;
import com.haredot.vo.LayuiPageResultVo;
import com.haredot.vo.R;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Controller
@RequestMapping("/teacher")
public class TeacherController {

    @Resource
    private TeacherService teacherService;

    /**
     * 教师信息展示页面
     *
     * @return
     */
    @GetMapping("/infos")
    @DenyStudent
    public String infos() {
        return "teacher";
    }

    /**
     * 添加教师信息页面
     *
     * @return
     */
    @GetMapping("/add")
    @DenyStudent
    public String addPage(ModelMap modelMap) {
        // 查询 所有的课程
        List<Course> list = teacherService.queryCourseList();
        modelMap.addAttribute("courses", list);
        return "teacherAdd";
    }

    /**
     * 添加教师
     */
    @PostMapping("/add")
    @ResponseBody
    @DenyStudent
    public R<Teacher> addTeacher(@RequestBody @Valid Teacher teacher) {
        return teacherService.save(teacher);
    }

    @GetMapping("/list")
    @ResponseBody
    @DenyStudent
    public LayuiPageResultVo<Teacher> teacherList(@RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
                                           @RequestParam(value = "limit", defaultValue = "10", required = false)Integer limit) {

        LayuiPage layuiPage = new LayuiPage(page, limit);

        R<Page<Teacher>> ret = teacherService.queryTeacherList(layuiPage);
        return LayuiPageResultVo.convertR(ret);
    }

    @DeleteMapping("/batch/remove")
    @ResponseBody
    @DenyStudent
    public R<?> removeBatch(@RequestBody List<Long> ids){
        if (ids.isEmpty()) {
            return R.fail(R.StatusCode.BAD).build();
        }
        return teacherService.batchRemove(ids);
    }

    @DeleteMapping("/remove/{id}")
    @ResponseBody
    @DenyStudent
    public R<?> deleteTeacher(@PathVariable("id") Long id){

        return teacherService.remove(id);
    }


    @GetMapping("/modify/{id}")
    @DenyStudent
    public String modifyPage(@PathVariable("id") Long sno, ModelMap modelMap) {
        // 根据 sno 查询学生信息、
        R<Teacher> ret = teacherService.queryTeacherById(sno);
        List<Course> list = teacherService.queryCourseList();
        if (ret.getStatus()) {
            modelMap.addAttribute("teacher", ret.getData());
        }
        modelMap.addAttribute("courses", list);
        return "teacherModify" ;
    }

    @PostMapping("/modify/{id}")
    @ResponseBody
    @DenyStudent
    public R<Teacher> modifyStudent(@PathVariable("id") Integer id, @RequestBody Teacher teacher) {

        if (!Objects.equals(teacher.getId(), id)) {
            return R.<Teacher>fail(R.StatusCode.BAD).data(Map.of("id", "参数错误"));
        }
        return teacherService.modify(teacher);
    }

    /**
     * 展示课程页面
     *
     * @return
     */
    @GetMapping("/course")
    @DenyStudent
    public String course() {
        return "course";
    }

    /**
     * 添加课程页面
     *
     * @return
     */
    @GetMapping("/course/add")
    @DenyStudent
    public String courseAdd() {
        return "courseAdd";
    }

    /**
     * 展示成绩页面
     *
     * @return
     */
    @GetMapping("/mark")
    @DenyStudent
    public String mark() {
        return "mark";
    }

    @GetMapping("/mark/{sno}")
    @ResponseBody
    @DenyStudent
    public R<Course> checkSnoAndQueryCourse(@PathVariable("sno") Long sno) {

        return teacherService.checkSnoAndQueryCourse(sno);
    }

    /**
     * 添加成绩页面
     *
     * @return
     */
    @GetMapping("/mark/add")
    @DenyStudent
    public String markAdd() {
        return "markAdd";
    }

    @PostMapping("/mark/add")
    @ResponseBody
    @DenyStudent
    public R<?> markAdd(@RequestBody Mark mark) {
        return teacherService.saveMark(mark);
    }

    @GetMapping("/mark/list")
    @ResponseBody
    @DenyStudent
    public LayuiPageResultVo<Map<String, Object>> list(@RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
                                           @RequestParam(value = "limit", defaultValue = "10", required = false)Integer limit,
                                           @RequestParam(value = "sno", required = false) Integer sno,
                                           @RequestParam(value = "courseName", required = false) String courseName) {

        Map<String, Object> params = new HashMap<>();
        params.put("sno", sno);
        params.put("courseName", courseName);

        LayuiPage layuiPage = new LayuiPage(page, limit);

        R<Page<Map<String, Object>>> ret = teacherService.queryMarksList(layuiPage, params);
        return LayuiPageResultVo.convertR(ret);
    }


    @DeleteMapping("/mark/remove/{id}")
    @ResponseBody
    @DenyStudent
    public R<?> deleteMark(@PathVariable("id") Integer id){
        return teacherService.removeMark(id);
    }

    @DeleteMapping("/mark/batch/remove")
    @ResponseBody
    @DenyStudent
    public R<?> removeMarkBatch(@RequestBody List<Integer> ids){
        if (ids.isEmpty()) {
            return R.fail(R.StatusCode.BAD).build();
        }
        return teacherService.batchMarkRemove(ids);
    }

    @GetMapping("/mark/modify{id}")
    @DenyStudent
    public String modifyMarkPage(@PathVariable("id") Integer id, ModelMap modelMap) {
        // 根据 sno 查询学生信息、
        R<Map<String, Object>> ret = teacherService.queryMarkById(id);

        if (ret.getStatus()) {
            modelMap.addAllAttributes(ret.getData());
        }
        return "markModify" ;
    }

    @PostMapping("/mark/modify{id}")
    @ResponseBody
    @DenyStudent
    public R<?> modifyMark(@PathVariable("id") Integer id, @RequestBody Mark mark) {

        if (!Objects.equals(mark.getId(), id)) {
            return R.<Student>fail(R.StatusCode.BAD).data(Map.of("id", "参数错误"));
        }
        return teacherService.modifyMark(mark);
    }

    /**
     * 添加课程
     *
     * @param course
     * @return
     */
    @PostMapping("/course/add")
    @ResponseBody
    @DenyStudent
    public R<Course> addCourse(@RequestBody @Valid Course course) {
        return teacherService.saveCourse(course);
    }

    @GetMapping("/course/list")
    @ResponseBody
    @DenyStudent
    public LayuiPageResultVo<Course> list(@RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
                                           @RequestParam(value = "limit", defaultValue = "10", required = false)Integer limit) {

        LayuiPage layuiPage = new LayuiPage(page, limit);

        R<Page<Course>> ret = teacherService.queryCourseList(layuiPage);
        return LayuiPageResultVo.convertR(ret);
    }


    @DeleteMapping("/course/batch")
    @ResponseBody
    @DenyStudent
    public R<?> batchRemoveCourse(@RequestBody List<Integer> ids){
        if (ids.isEmpty()) {
            return R.fail(R.StatusCode.BAD).build();
        }
        return teacherService.batchCourseRemove(ids);
    }


    @GetMapping("/course/modify{sno}")
    @DenyStudent
    public String modifyCourse(@PathVariable("sno") Integer sno, ModelMap modelMap) {
        // 根据 sno 查询学生信息、
        R<Course> ret = teacherService.queryCourseById(sno);

        if (ret.getStatus()) {
            modelMap.addAttribute("course", ret.getData());
        }
        return "courseModify" ;
    }

    @PostMapping("/course/modify{sno}")
    @ResponseBody
    @DenyStudent
    public R<Course> modifyCourse(@PathVariable("sno") Integer sno, @RequestBody Course course) {

        if (!Objects.equals(course.getId(), sno)) {
            return R.<Course>fail(R.StatusCode.BAD).data(Map.of("id", "参数错误"));
        }

        return teacherService.modifyCourse(course);
    }

    @DeleteMapping("/course/remove/{sno}")
    @ResponseBody
    @DenyStudent
    public R<?> removeCourse(@PathVariable("sno") Integer id){

        return teacherService.removeCourse(id);
    }
}
